package com.zbkj.admin.task.monitor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.monitor.ZfbMonitorEquipment;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.service.service.MerchantService;
import com.zbkj.service.service.monitor.IZfbMonitorEquipmentService;
import com.zbkj.service.service.monitor.IZfbMonitorOpenCloseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: MonitorOpenOrCloseTask
 * @Description: TODO
 * @Date: 2024/12/8 14:19
 * @Version: V1.0
 **/
@Slf4j
@Component("MonitorOpenOrCloseTask")
public class MonitorOpenOrCloseTask {


    @Autowired
    private IZfbMonitorOpenCloseService zfbMonitorOpenCloseService;


    @Autowired
    private MerchantService merchantService;
    @Autowired
    private IZfbMonitorEquipmentService zfbMonitorEquipmentService;

    /**
     * 1小时同步一次数据
     */
    public void checkOpenOrClose() {
        // cron : 0 0 */1 * * ?
        try {
            List<Merchant> list = merchantService.list();
            for (Merchant merchant : list) {
                List<ZfbMonitorEquipment> equipments = zfbMonitorEquipmentService.list(new LambdaQueryWrapper<ZfbMonitorEquipment>().eq(ZfbMonitorEquipment::getMerchantId, merchant.getId()));
                equipments.forEach(equipment -> {
                    boolean isOpen = zfbMonitorOpenCloseService.checkOpenOrClose(equipment.getLicense(), merchant.getId());
                    if (!isOpen) {
                        // 监控类型 1=免费  2=付费
                        Integer monitorType = 2;
                        // 免费时长 1=免费一个月 2=免费一个季度 3=免费一年 4=免费两年  5=免费三年
                        Integer freeMonitorTime = 0;
                        merchant.setMonitorType(monitorType);
                        merchant.setFreeMonitorTime(freeMonitorTime);
                        merchant.setUpdateTime(new Date());
                        merchantService.updateById(merchant);
                    }
                });

            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("MonitorOpenOrCloseTask.task" + " | msg : " + e.getMessage());
        }
    }
}
